package com.santu.edu.supporter.biz.order.service.impl;

import com.santu.edu.supporter.biz.good.service.GoodService;
import com.santu.edu.supporter.biz.order.dao.OrderDao;
import com.santu.edu.supporter.biz.order.pojo.dto.GoodInfo;
import com.santu.edu.supporter.biz.order.pojo.dto.OrderDTO;
import com.santu.edu.supporter.biz.order.pojo.enums.OrderStatusEnum;
import com.santu.edu.supporter.biz.order.pojo.params.OrderAddParam;
import com.santu.edu.supporter.biz.order.pojo.params.OrderUpdateParam;
import com.santu.edu.supporter.biz.order.service.OrderService;
import com.santu.edu.supporter.biz.pay.pojo.constants.WxNotifyConstants;
import com.santu.edu.supporter.biz.pay.pojo.dto.WxChatBasePayDTO;
import com.santu.edu.supporter.biz.pay.pojo.dto.WxChatPayDTO;
import com.santu.edu.supporter.biz.pay.service.WxOrderService;
import com.santu.edu.supporter.biz.user.service.UserService;
import com.santu.edu.supporter.exception.BizException;
import com.santu.edu.supporter.exception.CustomError;
import com.santu.edu.supporter.interceptor.UserThreadLocal;
import com.santu.edu.supporter.util.PageData;
import com.santu.edu.supporter.util.UuidUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author luoyaoqi
 * @createTime 2024/3/18
 * @description
 */
@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {

    private final UserService userService;

    private final OrderDao orderDao;

    private final GoodService goodService;

    private final TransactionTemplate transactionTemplate;

    private final WxOrderService wxOrderService;

    @Override
    public WxChatPayDTO addOrder(OrderAddParam addParam) {
        String userId = UserThreadLocal.get();
        String orderId = UuidUtils.getOrderId();
        return transactionTemplate.execute(status -> {
            WxChatPayDTO payDto = null;
            try {
                //判断库存并且减去库存
                List<GoodInfo> goodList = addParam.getGoodList();
                Boolean flag = goodService.lessGood(goodList);
                if (!flag) {
                    throw BizException.causeBy(CustomError.GOOD_NOT_ENOUGH);
                }
                OrderDTO dto = OrderDTO.createItem(userId, orderId, addParam);
                //调用微信支付API
                WxChatBasePayDTO basePayDto = WxChatBasePayDTO.builder()
                        .openId(userId)
                        .title("商品出售")
                        .orderId(orderId)
                        .price(dto.getPrice().divide(new BigDecimal(100)))
                        .notify(WxNotifyConstants.RUN_ERRANDS_NOTIFY)
                        .build();
                payDto = wxOrderService.pay(basePayDto);
                orderDao.addOrder(dto);
            } catch (BizException e) {
                //回滚
                status.setRollbackOnly();
            }
            return payDto;
        });
    }

    @Override
    public Boolean updateOrder(OrderUpdateParam updateParam) {
        orderDao.updateOrderAddress(updateParam.getOrderId(), updateParam.getAddressId());
        return true;
    }

    @Override
    public Boolean removeOrder(String orderId) {
        orderDao.removeOrder(orderId);
        return true;
    }

    @Override
    public PageData<OrderDTO> getOrderList(Integer orderStatus, Integer page, Integer size) {
        String userId = UserThreadLocal.get();
        return orderDao.getOrderList(userId, orderStatus, page, size);
    }

    @Override
    public void updateOrderStatus(String orderId, OrderStatusEnum newStatus, OrderStatusEnum oldStatus) {
        orderDao.updateOrderStatus(orderId,newStatus.getCode(),oldStatus.getCode());
    }

    @Override
    public OrderDTO getOrderById(String orderId) {
        return orderDao.getOrderById(orderId);
    }
}
